package com.gitee.T3;

import java.util.Scanner;

public class t3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("输入n和target：");
        int n = sc.nextInt();
        int target = sc.nextInt();
        int[] arr = new int[n];
        //假设输入数组已按升序排好
        System.out.println("输入数组：");
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        //二分法查找位置
        int head = 0;
        int tail = n - 1;
        while (true){
            int mid = (head + tail) / 2;
            if(arr[mid] == target){
                System.out.println(mid);
                break;
            } else if (arr[mid] > target) {
                tail = mid - 1;
            } else {
                head = mid + 1;
            }
            if(head > tail){
                System.out.println(head);
                break;
            }
        }
    }
}
